package org.jeecg.modules.work.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.fault.entity.BikeFaultSubmitFile;
import org.jeecg.modules.param.fault.mapper.BikeFaultSubmitFileMapper;
import org.jeecg.modules.param.fault.vo.SubmitFile;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecg.modules.work.dispatch.entity.BikeWorkDispatch;
import org.jeecg.modules.work.dispatch.mapper.BikeWorkDispatchMapper;
import org.jeecg.modules.work.dispatch.service.IBikeWorkDispatchService;
import org.jeecg.modules.work.file.entity.BikeWorkSubmitFile;
import org.jeecg.modules.work.file.mapper.BikeWorkSubmitFileMapper;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.jeecg.modules.work.repair.mapper.BikeWorkRepairMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: 调度工单
 * @Author: 月月鸟
 * @Date:   2021-05-31
 * @Version: V1.0
 */
@Service
public class BikeWorkDispatchServiceImpl extends ServiceImpl<BikeWorkDispatchMapper, BikeWorkDispatch> implements IBikeWorkDispatchService {

    @Resource
    private BikeWorkDispatchMapper bikeWorkDispatchMapper;
    @Resource
    private BikeWorkSubmitFileMapper bikeWorkSubmitFileMapper;
    @Value("${application.domain}")
    public String domain;
    @Override
    public Result<?> pageList(Integer pageNo, Integer pageSize, BikeWorkDispatch bikeWorkDispatch) {
        List<BikeWorkDispatch> bikeWorkDispatches = bikeWorkDispatchMapper.pageList(bikeWorkDispatch);
        for (BikeWorkDispatch sf : bikeWorkDispatches) {
            QueryWrapper<BikeWorkSubmitFile> wrapper = new QueryWrapper<>();
            wrapper.eq("work_id", sf.getId());
            List<BikeWorkSubmitFile> bikeWorkSubmitFiles = bikeWorkSubmitFileMapper.selectList(wrapper);
            //判断集合不为空
            if (bikeWorkSubmitFiles != null && !bikeWorkSubmitFiles.isEmpty()) {
                for (BikeWorkSubmitFile bfs : bikeWorkSubmitFiles) {
                    //判断连接不为空
                    if (StringUtils.isNotEmpty(bfs.getFileUrl())) {
                        if (sf.getFileUrl() != null) {
                            sf.setFileUrl(sf.getFileUrl() + "," + domain + bfs.getFileUrl());
                        } else {
                            sf.setFileUrl(domain + bfs.getFileUrl());
                        }
                    }
                }
            }

        }
        bikeWorkDispatches.sort(new Comparator<BikeWorkDispatch>() {
            @Override
            public int compare(BikeWorkDispatch o1, BikeWorkDispatch o2) {

                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return Result.OK(bikeWorkDispatches);
    }

    @Override
    public Result<?> pageListById(String id) {
        BikeWorkDispatch bikeWorkDispatch = bikeWorkDispatchMapper.pageListById(id);
        if (bikeWorkDispatch.getIsSolve().equals(BikeConstant.y_n_yes)||bikeWorkDispatch.getActualTime()!=null){
            bikeWorkDispatch.setIsFinish(BikeConstant.y_n_yes);
        }else {
            bikeWorkDispatch.setIsFinish(BikeConstant.y_n_no);
        }
        QueryWrapper<BikeWorkSubmitFile> fileWrapper = new QueryWrapper<>();
        //
        ////如果工单来源为重新派单
        //if (bikeWorkDispatch.getWorkSource().equals("4")){
        //    QueryWrapper<BikeWorkDispatch> wrapper = new QueryWrapper<>();
        //    wrapper.eq("source_id",bikeWorkDispatch.getSourceId());
        //    List<BikeWorkDispatch> bikeWorkDispatches = bikeWorkDispatchMapper.selectList(wrapper);
        //    //拼接图片
        //    for (BikeWorkDispatch bwd : bikeWorkDispatches){
        //        fileWrapper.eq("work_id",bwd.getId());
        //        List<BikeWorkSubmitFile> bikeWorkSubmitFiles = bikeWorkSubmitFileMapper.selectList(fileWrapper);
        //        for (BikeWorkSubmitFile bfs : bikeWorkSubmitFiles){
        //            if (bikeWorkDispatch.getId().equals(bfs.getWorkId())){
        //                if (bikeWorkDispatch.getFileUrl()!=null){
        //                    bikeWorkDispatch.setFileUrl(bikeWorkDispatch.getFileUrl()+","+bfs.getFileUrl());
        //                }else {
        //                    bikeWorkDispatch.setFileUrl(bfs.getFileUrl());
        //                }
        //            }
        //        }
        //    }
        //    return Result.OK(bikeWorkDispatches);
        //}else {
            //拼接工单图片
            fileWrapper.eq("work_id",bikeWorkDispatch.getId());
            List<BikeWorkSubmitFile> bikeWorkSubmitFiles = bikeWorkSubmitFileMapper.selectList(fileWrapper);
            for (BikeWorkSubmitFile bfs : bikeWorkSubmitFiles){
                if (bikeWorkDispatch.getId().equals(bfs.getWorkId())){
                    if (bikeWorkDispatch.getFileUrl()!=null){
                        bikeWorkDispatch.setFileUrl(bikeWorkDispatch.getFileUrl()+","+bfs.getFileUrl());
                    }else {
                        bikeWorkDispatch.setFileUrl(bfs.getFileUrl());
                    }
                }
            }
            return Result.OK(bikeWorkDispatch);
        //}

    }


    @Override
    public Result<?> deleteById(String id) {
        BikeWorkDispatch bikeWorkDispatch = bikeWorkDispatchMapper.selectById(id);
        if (bikeWorkDispatch.getIsDispatch().equals(BikeConstant.y_n_yes)){
            return Result.error("已派单，无法删除");
        }
        bikeWorkDispatchMapper.deleteById(id);
        return Result.OK("生成成功");
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request, BikeWorkDispatch bikeWorkDispatch) {

        List<BikeWorkDispatch> bikeWorkDispatches = bikeWorkDispatchMapper.pageList(bikeWorkDispatch);
        QueryWrapper<BikeWorkSubmitFile> wrapper = new QueryWrapper<>();
        List<BikeWorkSubmitFile> bikeWorkSubmitFiles = bikeWorkSubmitFileMapper.selectList(wrapper);

        for (BikeWorkDispatch sf : bikeWorkDispatches){
            for (BikeWorkSubmitFile bfs : bikeWorkSubmitFiles){
                if (sf.getId().equals(bfs.getWorkId())){
                    if (sf.getFileUrl()!=null){
                        sf.setFileUrl(sf.getFileUrl()+","+bfs.getFileUrl());
                    }else {
                        sf.setFileUrl(bfs.getFileUrl());
                    }
                }
            }
        }
        bikeWorkDispatches.sort(new Comparator<BikeWorkDispatch>() {
            @Override
            public int compare(BikeWorkDispatch o1, BikeWorkDispatch o2) {

                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return ExcelUtils.exportXls(request,bikeWorkDispatches,BikeWorkDispatch.class,"调度工单");
    }

    @Override
    public String judgeDelet(String[] split) {
        List<String> strings = Arrays.asList(split);
        for (String tb : strings){
            BikeWorkDispatch bikeWorkDispatch = bikeWorkDispatchMapper.selectById(tb);
            String isDispatch = bikeWorkDispatch.getIsDispatch();
            if (isDispatch.equals(BikeConstant.y_n_yes)){
                return BikeConstant.y_n_no;
            }
        }
        return BikeConstant.y_n_yes;
    }

    @Override
    public String judgeDispatch(BikeWorkDispatch bikeWorkDispatch) {
        BikeWorkDispatch bikeWorkDispatch1 = bikeWorkDispatchMapper.selectById(bikeWorkDispatch);
        if (bikeWorkDispatch1.getIsDispatch().equals(BikeConstant.y_n_yes)){
            return BikeConstant.y_n_no;
        }else {
            return BikeConstant.y_n_yes;
        }
    }
}
